//
//  SMACHomeController.swift
//  SmiraAiChat
//
//  Created by mumu on 2024/9/20.
//

import UIKit

class SMACHomeController: SMACBasicController {
    private lazy var homebg_SMAC: UIImageView = {
        let bgimage = UIImageView.init(image: UIImage.init(named: SMACUserManager.sharedSMAC.homebgSel.rawValue))
        bgimage.contentMode = .scaleAspectFill
        return bgimage
    }()
    
    private lazy var homeastro_SMAC: UIImageView = {
        let bgimage = UIImageView.init(image: UIImage.init(named: SMACUserManager.sharedSMAC.homeAstroSel.rawValue))
        bgimage.contentMode = .scaleAspectFill
        return bgimage
    }()
    
    private let mingavator = UIImageView.init(image: UIImage.init(named:SMACUserManager.sharedSMAC.logUser?.avatorname ?? ""))
    private let minename = UILabel.init()
    
    private lazy var makeupBtn_SMAC: UIButton = {
        let burron = UIButton.init()
        burron.setImage(UIImage.init(named: "icon_Dress up"), for: .normal)
        burron.setImage(UIImage.init(named: ""), for: .selected)
        return burron
    }()
    
    private lazy var playmusicBtn_SMAC: UIButton = {
        let burron = UIButton.init()
        burron.setImage(UIImage.init(named: "icon_MUSIC"), for: .normal)
        burron.setImage(UIImage.init(named: "icon_MUSIC_close"), for: .selected)
        return burron
    }()
    
    private lazy var AIBtn_SMAC = UIButton.init()
    private lazy var exploreBtn_SMAC = UIButton.init()
    lazy var sayView_SMAC = SMACInputSayView.init()
    lazy var dialogBoxView_SMAC: UITableView = {
        let dialogBox = UITableView.init(frame: .zero, style: .plain)
        dialogBox.dataSource = self
        dialogBox.delegate = self
        dialogBox.register(SMACInputSayCell.self, forCellReuseIdentifier: "SMACInputSayCellID")
        dialogBox.separatorStyle = .none
        dialogBox.allowsSelection = false
        dialogBox.rowHeight = UITableView.automaticDimension
        dialogBox.estimatedRowHeight = 32
        dialogBox.backgroundColor = .clear
        return dialogBox
    }()
    
    
    
    var randomTopUsers = Array<SMACUserMoadel>.init()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupNormaUIView_AMSC()
    }

}
extension SMACHomeController:UITableViewDelegate,UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return SMACUserManager.sharedSMAC.myplaneDiaologArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SMACInputSayCellID", for: indexPath) as! SMACInputSayCell
        let diolog = SMACUserManager.sharedSMAC.myplaneDiaologArray[indexPath.row]
        
        cell.isMeSay = diolog.isme
        cell.contentLbl_SMAC.text = diolog.content
        return cell
        
    }
    
}

extension SMACHomeController{
    func setupNormaUIView_AMSC(){
        mingavator.layer.cornerRadius = 17.x_bili
        mingavator.layer.masksToBounds = true
        
        
        minename.text = SMACUserManager.sharedSMAC.logUser?.username
        minename.font = UIFont(name: "HarmonyOS Sans Medium", size: 16.x_bili)
        
       
        
        let AIvator = UIImageView.init(image: UIImage.init(named: "home_icon_AI"))
        let findvator = UIImageView.init(image: UIImage.init(named: "home_icon_find"))
        let aititle = UILabel.init()
        aititle.text = "AstroMate AI"
        aititle.font = UIFont(name: "HarmonyOS Sans Medium", size: 12.x_bili)
        aititle.textAlignment = .center
        let findtitle = UILabel.init()
        findtitle.text = "Explore Space"
        findtitle.font = UIFont(name: "HarmonyOS Sans Medium", size: 12.x_bili)
        findtitle.textAlignment = .center
     
        view.addSubview(homebg_SMAC)
        view.addSubview(homeastro_SMAC)
        
        view.addSubview(mingavator)
        view.addSubview(minename)
        view.addSubview(playmusicBtn_SMAC)
        view.addSubview(makeupBtn_SMAC)
        
        createTopAvtorBanner()
        view.addSubview(AIvator)
        view.addSubview(aititle)
        view.addSubview(findvator)
        view.addSubview(findtitle)
        
        view.addSubview(AIBtn_SMAC)
        view.addSubview(exploreBtn_SMAC)
        
        view.addSubview(sayView_SMAC)
        
    }
    
    
    func createTopAvtorBanner() {
        let banner = UIView.init()
        banner.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha:0.8)
        banner.layer.cornerRadius = 12
        
        let mineID = SMACUserManager.sharedSMAC.logUser?.userID
        randomTopUsers = SMACUserManager.sharedSMAC.getRandomUserWith(count: 4, excrepID: mineID ?? "")
        for (i,item) in randomTopUsers.enumerated() {
            let burron = UIButton.init()
            burron.setImage(UIImage.init(named:item.avatorname ?? ""), for: .normal)
            burron.layer.cornerRadius = 52.x_bili/2
            burron.layer.masksToBounds = true
            burron.tag = i
            banner.addSubview(burron)
        }
    }
   
}
